package com.zzb.demo.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * @Author zzb
 * @Date 2024/10/8 10:44
 * @Version 1.0
 * @description
 */
public class MybatisUtil {
    private static SqlSessionFactory sessionFactory = null;

    static {
        Reader reader = null;
        String resource = "mybatis-config.xml";
        try {
            reader = Resources.getResourceAsReader(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    }

    public static SqlSession getSqlSession() throws IOException {

        return sessionFactory.openSession(true);
    }
}
